<template>
    <div class="search-div">
        <!-- 搜索表单 -->
        <el-form label-width="70px" size="small">
            <el-form-item label="科室名称">
                <el-input v-model="division.sname" style="width: 120px" placeholder="科室"></el-input>
            </el-form-item>
            <el-row style="display:flex">
                <el-button type="primary" size="small" @click="searchSysRole">
                    搜索
                </el-button>
                <el-button size="small" @click="resetData">重置</el-button>
            </el-row>
        </el-form>
        <!-- 添加按钮 -->
        <div class="tools-div">
            <el-button type="success" size="small" @click="addShow">添 加</el-button>
        </div>

        <!--- 角色表格数据 -->
        <el-table :data="list" style="width: 100%">

            <el-table-column prop="sname" label="科室名称" />
            <el-table-column prop="mname" label="治疗病症" />
            <el-table-column prop="createTime" label="创建时间" />
            <el-table-column prop="updateTime" label="修改时间" />
            <el-table-column label="操作" align="center" width="280" #default="scope">
                <el-button type="primary" size="small" @click="editShow(scope.row)">修改</el-button>
                <el-button type="danger" size="small" @click="deleteById(scope.row)">删除</el-button>
            </el-table-column>
        </el-table>

        <!--分页条-->
        <!-- <el-pagination v-model:current-page="pageParams.page" v-model:page-size="pageParams.limit"
            :page-sizes="[10, 20, 50, 100]" @size-change="fetchData" @current-change="fetchData"
            layout="total, sizes, prev, pager, next" :total="total" /> -->
        <!-- 添加角色表单对话框 -->
        <el-dialog v-model="dialogVisible" title="添加或修改" width="30%">
            <el-form label-width="120px">
                <el-form-item label="科室名称">
                    <el-input v-model="division.sname" />
                </el-form-item>
                <!-- <el-form-item label="治疗病症">
                    <el-checkbox-group v-model="checkList">
                        <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
                    </el-checkbox-group>
                </el-form-item> -->
                <el-form-item>
                    <el-button type="primary" @click="submit">提交</el-button>
                    <el-button @click="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { GetDivisionListByPage,findDivision, SaveDivision, UpdateDivision, DeleteDivisionById } from '@/api/division/division';
import {GetCompilation} from '@/api/compilations';
import { ElMessage, ElMessageBox } from 'element-plus'

//分页条件总记录数
let total = ref(0)

//定义表格数据模型
let list = ref([])

//分页数据
const pageParamsForm = {
    page: 1,//页码
    limit: 5, //每页记录数
}

const pageParams = ref(pageParamsForm)

const searchSysRole = () => {
    fetchData();
}

const resetData = () => {
    division.value.sname = ""
    fetchData();
}


// 控制对话是否展示的变量
const dialogVisible = ref(false)

//表单数据模型
const defaultForm = {
    id: "",
    sname: "",
    mname: "",
    createTime: "",
    updateTime: "",
    states: "",
    isDeleted: ""
}
const division = ref(defaultForm)

//钩子函数
onMounted(() => {
    fetchData();
})

const fetchData = async () => {
    const { data, code, message } = await findDivision();
    list.value = data;
    //total.value = data.total;
}

//进入添加
const addShow = () => {
    dialogVisible.value = true
    division.value = {}
}

//修改页面
const editShow = (row) => {
    division.value = row
    dialogVisible.value = true
}

//添加设备
const submit = async () => {
    if (!division.value.id) {
        const { code } = await SaveDivision(division.value);
        console.log(division.value);
        if (code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            division.value = {}
            fetchData()
        }
    } else {
        const { code } = await UpdateDivision(division.value);
        if (code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            division.value = {}
            fetchData()
        }
    }
}

//删除设备
const deleteById = (row) => {
    ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const { code } = await DeleteDivisionById(row.id)
        if (code === 200) {
            ElMessage.success('删除成功')
            pageParams.value.page = 1
            fetchData()
        }
    })
}

</script>

<style scoped>
.search-div {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}

.tools-div {
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}
</style>